#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.contrib.auth.models import User, check_password

class MailBackend:
    """
    Backend that allows login with mailadress
    for more info on custom backends see :
    http://docs.djangoproject.com/en/dev/topics/auth/#other-authentication-sources
    """
    def authenticate(self, username=None, password=None):
        
        if User.objects.filter(email=username):   # but would look ugly 
                user = User.objects.get(email=username)
        else : 
                return None;
        
        if user.check_password(password) :
                return user;
            


    def get_user(self, id):
        if User.objects.filter(id=id):
            return User.objects.get(id=id)
        else :
            return None

        

